﻿using KinonekoSoftware.CodariaDB.Documents.Personalities;
using KinonekoSoftware.Studio.Builders;
using KinonekoSoftware.Studio.Models.DataParts.Personality;

namespace KinonekoSoftware.Studio.Pages.Notions
{

        public sealed class EvaluationAsInspirationGalleryViewModel : RepositoryGalleryItem
        {
            private readonly CompositeEngine     _engine_sentence;
            private readonly InstanceCacheEngine _engine_cache;
            private readonly List<Evaluation>      _collection;

            private EvaluationPresenter _current;
            private int        _version;
            private int        _index;

            public EvaluationAsInspirationGalleryViewModel()
            {
                Platform.GetEngine<CompositeEngine, InstanceCacheEngine>(out _, out _engine_sentence, out _engine_cache);
                _collection = new List<Evaluation>(32);
                _index      = 0;


                PreviousItemCommand = CreateCommand(PreviousItem);
                RandomItemCommand   = CreateCommand(RandomItem);
                NextItemCommand     = CreateCommand(NextItem);
            }

            protected override void OnStart()
            {
                _collection.AddMany(_engine_sentence.GetEvaluations(), true);
                _version = _engine_sentence.Version;
                var eva = _collection.FirstOrDefault();
                Current = EvaluationBuilder.Build(eva, _engine_cache);
                base.OnStart();
            }

            protected override void OnResume()
            {
                if (_version != _engine_sentence.Version)
                {
                    _collection.AddMany(_engine_sentence.GetEvaluations(), true);
                    _version = _engine_sentence.Version;
                    var eva = _collection.FirstOrDefault();
                    Current = EvaluationBuilder.Build(eva, _engine_cache);
                }

                base.OnResume();
            }

            private EvaluationPresenter Build(int index) => EvaluationBuilder.Build(_collection[index], _engine_cache);

            private void PreviousItem()
            {
                if (_collection.Count <= 0)
                {
                    return;
                }

                _index  = (_index - 1) % _collection.Count;
                Current = Build( _index);
            }

            private void RandomItem()
            {

                if (_collection.Count <= 0)
                {
                    return;
                }

                _index  = Random.Shared.Next(0, _collection.Count - 1);
                Current = Build( _index);

            }

            private void NextItem()
            {
                if (_collection.Count <= 0)
                {
                    return;
                }

                _index  = (_index + 1) % _collection.Count;
                Current = Build( _index);
            }


            public ICommandEX PreviousItemCommand { get; }
            public ICommandEX RandomItemCommand   { get; }
            public ICommandEX NextItemCommand     { get; }

            public InstanceCache CurrentEvaluationFrom => Current?.From;
            public InstanceCache CurrentEvaluationTo   => Current?.To;

            public EvaluationPresenter Current
            {
                get => _current;
                set
                {
                    SetValue(ref _current, value);
                    RaiseUpdated(nameof(CurrentEvaluationFrom));
                    RaiseUpdated(nameof(CurrentEvaluationTo));
                }
            }
        }
}